package com.huanbao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanbao.entity.PersonalIntegralDetail;
import com.huanbao.entity.PersonalIntegralNumber;
import com.huanbao.entity.PersonalMessage;
import com.huanbao.entity.User;
import com.huanbao.mapper.PersonalIntegralDetailMapper;
import com.huanbao.mapper.PersonalIntegralNumberMapper;
import com.huanbao.mapper.PersonalMessageMapper;
import com.huanbao.mapper.UserMapper;
import com.huanbao.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 环宝用户表 服务实现类
 * </p>
 *
 * @author zq
 * @since 2021-04-01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private PersonalMessageMapper messageMapper;

    @Resource
    private PersonalIntegralDetailMapper integralDetailMapper;

    @Resource
    private PersonalIntegralNumberMapper integralNumberMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public User findAccount(long account) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("account", account));
    }

    @Override
    public void createMessage(String message, Long userId) {
        PersonalMessage personalMessage = new PersonalMessage();
        personalMessage.setUserId(userId)
                .setContent(message);
        messageMapper.insert(personalMessage);
    }

    @Override
    public void IntegralModify(Long userId, int state, int number) {
        PersonalIntegralDetail personalIntegralDetail = new PersonalIntegralDetail();
        personalIntegralDetail.setType(state)
                .setNumber(number)
                .setUserId(userId);
        integralDetailMapper.insert(personalIntegralDetail);
        // 0 获得
        if (state == 0) state = 1;
            // 1 消耗
        else state = -1;
        PersonalIntegralNumber integralNumber = integralNumberMapper.selectOne(new QueryWrapper<PersonalIntegralNumber>().eq("user_id", userId));
        integralNumber.setIntegralNumber(integralNumber.getIntegralNumber() + state * number);
        integralNumberMapper.updateById(integralNumber);
    }


}
